/*
 * 用户行为数据搜索
 * @Author: along
 * @Date: 2020-03-30
 */
<template>
    <div class="container hireSetBood">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left"/>
            <div class="header-right">
                <div class="header-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="fnAddBood()"
                    >
                        新建保证金
                    </el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="hireSetBood"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="level_money"
                label="保证金"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="level_logo"
                label="等级icon"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <img
                        :src="scope.row.level_logo"
                        style="width:25px;height:25px;border-radius:50%"
                    >
                </template>
            </el-table-column>
            <el-table-column
                prop="level_remark"
                label="等级备注"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                label="操作"
                align="center"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnEditbood(scope.row)"
                    >
                        编辑
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnDeleteBood(scope.row)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <!-- <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div> -->

        <!-- 新建/编辑保证金 -->
        <el-dialog
            :visible.sync="dialogVisibleBood"
            width="400px"
            :close-on-click-modal="true"
            center
        >
            <el-form
                :model="ruleForm"
                :rules="rules"
                ref="ruleForm"
                label-width="100px"
                class="demo-ruleForm"
            >
                <el-form-item
                    label="保证金金额"
                    prop="money"
                >
                    <el-input
                        v-model="ruleForm.money"
                        size="small"
                        style="width:90%"
                        type="number"
                        min="0"
                    />
                    <span>元</span>
                </el-form-item>
                <el-form-item
                    label="等级图标"
                    prop="icon"
                >
                    <el-upload
                        :show-file-list="false"
                        :http-request="e=> {fnUploadImageOfCustomerLogo(e)}"
                        accept="image/*"
                        class="upload-demo"
                        drag
                        :on-exceed="e=>{fnExceed(e)}"
                        action="https://jsonplaceholder.typicode.com/posts/"
                    >
                        <img
                            v-if="ruleForm.icon"
                            :src="ruleForm.icon"
                            class="avatar"
                        >
                        <el-button
                            v-if="ruleForm.icon == ''"
                            type="primary"
                            size="mini"
                        >
                            点击上传
                        </el-button>
                        <!-- <p
                            class="el-icon-error-tip"
                            v-if="ruleForm.icon"
                            @click.stop="fnDelete()"
                        >
                            <i class="el-icon-error"/>
                        </p> -->
                    </el-upload>
                </el-form-item>
                <el-form-item
                    label="等级备注"
                    prop="remark"
                >
                    <el-input
                        v-model="ruleForm.remark"
                        size="small"
                    />
                </el-form-item>
            </el-form>
            <span
                slot="footer"
                class="dialog-footer">
                <el-button
                    type="primary"
                    size="small"
                    @click="fnConfirmBood('ruleForm')"
                >
                    保存
                </el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { back } from '@/mixin/back';
export default {
    mixins: [back],
    data () {
        return {
            hireList: [],
            level_no: '',
            type: '',
            rules: {
                money: [
                    { required: true, message: '请填写保证金金额,不可以为空', trigger: 'change' }
                ],
                icon: [
                    { required: true, message: '请上传等级图标,不可以为空', trigger: 'change' }
                ]
            },
            ruleForm: {
                money: '',
                icon: '',
                remark: ''
            },
            dialogVisibleBood: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {}
        };
    },
    filters: {},
    computed: {},
    watch: {
        dialogVisibleBood (val) {
            if(!val) {
                this.ruleForm = {
                    money: '',
                    icon: '',
                    remark: ''
                };
                this.$refs['ruleForm'].resetFields();
            }
        }
    },
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "广告系统"},{title: '设置保证金'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();

        let page = 1;

        this.getHideList(page);
    },
    methods: {
        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/hire_level%5Cget_list', {
                page_id: page || this.page.now_page
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    // this.page.now_page = resp.data.page.now_page;
                    // this.page.total_count = resp.data.page.total_count;
                    // this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 递归获取所有企业数据,用于删除判断
         */
        getHideList (page) {
            this.$post('/wei4/hire_user%5Cget_list', {
                page_id: page
            }, resp => {
                if (resp.code == 1) {
                    if(resp.data.page.next_page !== 0) {
                        this.hireList = this.hireList.concat(resp.data.list);
                        this.getHideList(page + 1);
                    } else {
                        this.hireList = this.hireList.concat(resp.data.list);
                        return;
                    }

                }
            });
        },

        /**
         * 上传等级图标
         */
        fnUploadImageOfCustomerLogo(e) {
            this.fnUploadImage(e, (resp) => {
                this.ruleForm.icon = resp.data.url;
            }, (resp) => {
                console.log(resp.msg);
            });
        },

        fnExceed(e) {
            if(this.ruleForm.icon !== '') {
                this.$message({
                    type: 'warning',
                    message: '最多只可以上传一张',
                    center: true
                });
                return;
            }
        },

        fnDelete() {
            this.$set(this.ruleForm, 'icon', '');
            return;
        },

        fnUploadImage(e, success, error) {
            let timeTmp = new Date().getTime();
            let pic = new FormData();

            pic.append('file', e.file);
            this.$upload({
                url: '/__local/common%5Cupload',
                data: pic,
                success: resp => {
                    if (resp.code == 1) {
                        success(resp);
                    } else {
                        this.$message({
                            type: 'warning',
                            message: '上传图片失败',
                            center: true
                        });
                        error(resp);
                    }
                }
            });
        },

        /**
         * 新建保证金
         */
        fnAddBood () {
            this.type = 'add';
            this.dialogVisibleBood = !this.dialogVisibleBood;
        },

        /**
         * 编辑保证金
         */
        fnEditbood (row) {
            this.type = 'edit';
            this.ruleForm = {
                money: row.level_money,
                icon: row.level_logo,
                remark: row.level_remark
            };
            this.level_no = row.level_no;
            this.dialogVisibleBood = !this.dialogVisibleBood;
        },

        /**
         * 确定选择保证金
         */
        fnConfirmBood (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if(this.type == 'add') {
                        this.$post('/student/hire_level%5Cadd',{
                            level_money: this.ruleForm.money,
                            level_logo: this.ruleForm.icon,
                            level_remark: this.ruleForm.remark
                        }, resp => {
                            if (resp.code == 1) {
                                this.getTableData(1);
                                this.dialogVisibleBood = !this.dialogVisibleBood;
                                this.$message({
                                    type: 'success',
                                    message: '新建成功',
                                    center: true
                                });
                            } else {
                                this.$message({
                                    type: 'warning',
                                    message: resp.msg,
                                    center: true
                                });
                            }
                        });
                    } else if (this.type == 'edit') {
                        this.$post('/student/hire_level%5Cset',{
                            level_no: this.level_no,
                            level_money: this.ruleForm.money,
                            level_logo: this.ruleForm.icon,
                            level_remark: this.ruleForm.remark
                        }, resp => {
                            if (resp.code == 1) {
                                this.getTableData(1);
                                this.dialogVisibleBood = !this.dialogVisibleBood;
                                this.$message({
                                    type: 'success',
                                    message: '编辑成功',
                                    center: true
                                });
                            } else {
                                this.$message({
                                    type: 'warning',
                                    message: resp.msg,
                                    center: true
                                });
                            }
                        });
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        /**
         * 删除保证金
         */
        fnDeleteBood (row) {
            for(let i=0; i<this.hireList.length; i++) {
                if(this.hireList[i].level_no == row.level_no) {
                    this.$message({
                        type: 'warning',
                        message: '存在合作客户设置了该等级，请先将其等级变更后才可删除',
                        center: true
                    });
                    return;
                }
            }

            this.$confirm('是否删除该条数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/hire_level%5Cdelete',{
                    level_no: row.level_no
                }, resp => {
                    if (resp.code == 1) {
                        this.getTableData(1);
                        this.$message({
                            type: 'success',
                            message: '删除成功',
                            center: true
                        });
                    } else {
                        this.$message({
                            type: 'warning',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
            }
            .header-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    .el-icon-error-tip {
        position: absolute;
        top: -13px;
        right: -10px;
        background: #fff;
        border-radius: 50%;
        overflow: hidden;
        z-index: 999;
        .el-icon-error {
            font-size: 25px;
            color: #333;
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
</style>
<style lang="less">
    .hireSetBood {
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
        .el-upload-dragger {
            width: auto;
            height: auto;
            border: none;
            border-radius: 0px;
            overflow: visible;
        }
        .avatar {
            min-width: 15px;
            max-width: 226px;
        }
        .el-form-item__content {
            line-height: 0;
        }
        .el-form-item__content {
            margin-top: 5px;
        }
    }
</style>